<template>
	<wd-toast />
	<BpmComponent :processInstanceId="processInstanceId">
		<template #approval-info>
			<up-form labelPosition="left" labelWidth="120" v-if="Object.keys(formData).length > 0">
				<up-form-item v-for="field in formFields" :key="field.key" :label="field.label" borderBottom>
					<!-- 修改正文字段显示方式 -->
					<text v-if="field.isDocument && formData[field.key]" class="file-link"
						@click="previewFile(formData[field.key])">
						{{ formData.ename?.slice(0, 30) || '无' }}
					</text>
					<text v-else>
						{{ field.format ? field.format(formData[field.key]) : formData[field.key] || '无' }}
						<!-- {{ formData[field.key] }} -->
					</text>
				</up-form-item>
			</up-form>
		</template>
	</BpmComponent>
</template>

<script setup lang="ts">
import { onLoad } from "@dcloudio/uni-app";
import { ref, unref, reactive, nextTick, getCurrentInstance } from 'vue'
import { DICT_TYPE, getIntDictOptions, getDictLabel } from '@/utils/dict'
import { previewFile } from '@/utils/fileUtils'
import BpmComponent from '@/components/processInstance/detail/index.vue'
import * as LeaveApi from '@/api/bpm/leave'
import { useMessage, useToast } from 'wot-design-uni'
import { formatDate } from '@/utils/formatTime'

const message = useMessage()
const toast = useToast()

const processInstanceId = ref(null)// 流程实例的编号
const detailId = ref(null)
const formData = ref<any>({} as any)


const formFields = [
	{
		label: '请假类型', key: 'type', format: (val: any) => {
			const dicts = getIntDictOptions(DICT_TYPE.BPM_OA_LEAVE_TYPE)
			const dict = dicts.find(item => item.value === val)
			return dict ? dict.label : '-'
		}
	},
	{ label: '开始时间', key: 'startTime', format: formatDate },
	{ label: '结束时间', key: 'endTime', format: formatDate },
	{ label: '原因', key: 'reason' },
	{ label: '附件', key: 'attachments', isDocument: true }
]

onLoad(async (options) => {
	processInstanceId.value = options?.processInstanceId
	detailId.value = options?.id
	/** 获得详情 */
	getDetailById()
})
/** 获取页面详情 */
const getDetailById = async () => {
	try {
		const data = await LeaveApi.getLeave(Number(detailId.value))
		formData.value = data
		if (formData.value.url) {
			formData.value.ename = formData.value.url.split('/').pop() || '附件'
		}
	} catch (error) {
		console.log('加载数据失败')
	}
}
</script>

<style>
/* 样式 */
/* 新增文件链接样式 */
.file-link {
	color: #007AFF;
	text-decoration: underline;
	cursor: pointer;
}

.uv-node {
	height: 34rpx;
	width: 34rpx;
	border-radius: 50%;
	background-color: #ddd;
	display: flex;
	align-items: center;
	justify-content: center;
	color: #fff;
	flex-shrink: 0;
	z-index: -1;
}

.audit-node {
	margin: 5px 0;
}
</style>